package cn.jishiyu11.xeqb.adapter

import android.content.Context
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import cn.jishiyu11.xeqb.EntityClass.FragmengData.DiscoverBean
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.utils.ImageLoadUtils
import cn.jishiyu11.xeqb.webview.X5WebViewActivity

/**
 * Created by jsy_zj on 2018/1/14.
 */
class DiscoverRecyclerAdapter(context: Context, infoList: MutableList<DiscoverBean.Discover>?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var context: Context? = null
    var list: MutableList<DiscoverBean.Discover>? = null
    var inflater: LayoutInflater? = null
    val TYPE_FOOT = 0
    val TYPE_NORMAL = 1

    init {
        this.context = context
        this.list = infoList
        this.inflater = LayoutInflater.from(this.context)
    }


    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        var view:View? = null
        when(viewType){
            TYPE_FOOT->{
                view = inflater?.inflate(R.layout.footermore,parent,false)
                return ViewHolderLoadMore(view,context!!)
            }
            else->{
                view = inflater?.inflate(R.layout.home_recycler_item_text1,parent,false)
                return ViewHolder(view,context!!)
            }
        }

        return ViewHolder(view, context!!)
    }

    override fun getItemCount(): Int {
        return if (list?.size != null && list?.size!! > 0) list?.size!! + 1 else 0
    }

    override fun getItemViewType(position: Int): Int {
//        return super.getItemViewType(position)
        var type = 0
        if (position >= list?.size!!) {
            type = TYPE_FOOT
        } else {
            type = TYPE_NORMAL
        }
        return type
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        if (position >= list?.size!!) {

        } else {
            if (holder is ViewHolder) {
                var type = list?.get(position)?.type
                when (type) {
                    "text" -> {

                        holder?.recycler_view_item1?.visibility = View.VISIBLE
                        holder?.recycler_view_item2?.visibility = View.GONE

                        var picurl = list?.get(position)?.pic
                        picurl?.let { ImageLoadUtils.display(context!!, holder?.home_msg_img, it) }
                        holder?.home_msg_title?.text = list?.get(position)?.title
                        holder?.home_msg_content?.text = list?.get(position)?.intro
                        holder?.home_msg_time?.text = list?.get(position)?.logtime

                        holder?.recycler_view_item1!!.setOnClickListener {
                            var intent = Intent(this.context, X5WebViewActivity::class.java)
                            intent.putExtra("url", list?.get(position)?.url)
                            this.context?.startActivity(intent)
                        }

                    }
                    "pic" -> {

                        holder?.recycler_view_item1?.visibility = View.GONE
                        holder?.recycler_view_item2?.visibility = View.VISIBLE
                        var picurl = list?.get(position)?.pic
                        picurl?.let { ImageLoadUtils.display(context!!, holder?.home_msg_bigimg, it) }
                        holder?.recycler_view_item2!!.setOnClickListener {
                            var intent = Intent(this.context, X5WebViewActivity::class.java)
                            intent.putExtra("url", list?.get(position)?.url)
                            this.context?.startActivity(intent)
                        }
                    }
                }
            }
        }
    }


    class ViewHolder(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var home_msg_img: ImageView = itemView?.findViewById(R.id.home_msg_img)!!
        var home_msg_title: TextView = itemView?.findViewById(R.id.home_msg_title)!!
        var home_msg_content: TextView = itemView?.findViewById(R.id.home_msg_content)!!
        var home_msg_time: TextView = itemView?.findViewById(R.id.home_msg_time)!!
        var home_msg_bigimg: ImageView = itemView?.findViewById(R.id.home_msg_bigimg)!!

        var recycler_view_item1: LinearLayout = itemView?.findViewById(R.id.recycler_view_item1)!!
        var recycler_view_item2: LinearLayout = itemView?.findViewById(R.id.recycler_view_item2)!!

    }

    class ViewHolderLoadMore(itemView: View?, context: Context) : RecyclerView.ViewHolder(itemView) {
        var gridView: LinearLayout = itemView?.findViewById(R.id.footer_layout)!!
    }


}